package com.mz.nhoz.xlsx;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * Interprete de valores de celdas.
 * 
 * @author martin
 *
 */
class CellValueInterpreter {
	XSSFWorkbook workbook;

	private CellValueInterpreter() {
	};

	CellValueInterpreter(XlsxSimpleVertTableReader tableReader) {
		this(tableReader.workbook);
	}

	CellValueInterpreter(XSSFWorkbook workbook) {
		this.workbook = workbook;
	}

	public Object getValue(Cell cell) {
		FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();

		switch (cell.getCellType()) {
			case Cell.CELL_TYPE_NUMERIC:
				return new Double(cell.getNumericCellValue());
			case Cell.CELL_TYPE_STRING:
				return cell.getStringCellValue();
			case Cell.CELL_TYPE_BOOLEAN:
				return new Boolean(cell.getBooleanCellValue());
			case Cell.CELL_TYPE_ERROR:
				return Byte.valueOf(cell.getErrorCellValue());

			case Cell.CELL_TYPE_FORMULA:
				CellValue cellValue = evaluator.evaluate(cell);

				switch (cellValue.getCellType()) {
					case Cell.CELL_TYPE_NUMERIC:
						return new Double(cellValue.getNumberValue());
					case Cell.CELL_TYPE_STRING:
						return cellValue.getStringValue();
					case Cell.CELL_TYPE_BOOLEAN:
						return new Boolean(cellValue.getBooleanValue());
					case Cell.CELL_TYPE_ERROR:
						return Byte.valueOf(cellValue.getErrorValue());
				}
		}

		return null;
	}
}// XlsxCellValue
